package org.BooCrack.bookstore.pay.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.BooCrack.bookstore.common.pojo.BSResult;
import org.BooCrack.bookstore.common.pojo.ZTreeNode;
import org.BooCrack.bookstore.common.utils.Response;
import org.BooCrack.bookstore.common.utils.ResponseResult;
import org.BooCrack.bookstore.model.entity.Privilege;
import org.BooCrack.bookstore.model.service.IPrivilegeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//@RequestMapping("admin/privilege")
@RequestMapping("admin/V5")
@RequiresPermissions("privilege-manage")
@Api(value = "v1", tags = "8-3.后台管理系统管理权限模块接口")
public class PrivilegeController {


    @Autowired
    private IPrivilegeService privilegeService;

    @ResponseBody
    @GetMapping("/privilege")
    @ApiOperation(value = "权限列表", notes = "权限列表")
    public ResponseResult<List<ZTreeNode>> treeNodesJsonData(){
//        return privilegeService.getZTreeNodes();
        return Response.createOkResp(privilegeService.getZTreeNodes());

    }

    @ResponseBody
    @GetMapping("/rolePrivileges/{roleId}")
    @ApiOperation(value = "根据id查看角色权限信息", notes = "根据id查看角色权限信息")
    public ResponseResult<List<ZTreeNode>> treeRolePrivileges(@PathVariable("roleId") int roleId){
//        return privilegeService.getRolePrivileges(roleId);
        return Response.createOkResp(privilegeService.getRolePrivileges(roleId));
    }


    @ResponseBody
    @GetMapping("/privilege/{privId}")
    @ApiOperation(value = "根据id查看用户权限信息", notes = "根据id查看用户权限信息")
    public ResponseResult<BSResult> getPrivilege(@PathVariable("privId") int privId){
//        return privilegeService.findById(privId);
        return Response.createOkResp(privilegeService.findById(privId));

    }


    @PutMapping("/rolePrivileges/{roleId}")
    @RequiresPermissions("privilege-edit")
    @ApiOperation(value = "根据id修改角色权限信息", notes = "根据id修改角色权限信息")
    public ResponseResult<Map<String, Object>> toEditPrivilege(@PathVariable("roleId") int roleId, Model model){
        model.addAttribute("roleId", roleId);
//        return "admin/privilege/edit";

        Map<String, Object> map = new HashMap<>();
        map.put("roleId", roleId);
        return Response.createOkResp("roleId",map);
    }


    @ResponseBody
    @PutMapping("/privilege")
    @RequiresPermissions("privilege-edit")
    @ApiOperation(value = "修改权限", notes = "修改权限")
    public ResponseResult<BSResult> editPrivilege(Privilege privilege){

        BSResult bsResult = privilegeService.updatePrivilege(privilege);

//        return bsResult;
        return Response.createOkResp(bsResult);

    }

    @GetMapping("/privilege/list")
    @RequiresPermissions("privilege-list")
    @ApiOperation(value = "权限列表", notes = "权限列表")
    public String privilegeList(){
        return "admin/privilege/list";
    }

    @PostMapping("/privilege")
    @ResponseBody
    @RequiresPermissions("privilege-add")
    @ApiOperation(value = "添加权限", notes = "添加权限")
    public ResponseResult<BSResult> addPrivilege(Privilege privilege){
        //添加权限，仅有权限名字，之后再修改
        BSResult bsResult = privilegeService.addPrivilege(privilege);
        return Response.createOkResp(bsResult);

    }

    @DeleteMapping("/privilege/{privId}")
    @ResponseBody
    @RequiresPermissions("privilege-delete")
    @ApiOperation(value = "删除权限", notes = "删除权限")
    public ResponseResult<BSResult> deletePrivilege(@PathVariable("privId") int privId){
        BSResult bsResult = privilegeService.deleteById(privId);
        return Response.createOkResp(bsResult);

    }

}
